package com.xsm.activitygroup;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import com.xsm.internet.PostMessage;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.ContactsContract.Contacts.Data;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Qiandao extends Activity{
	Context context;
	Button button;
	QHandler handler;
	String  user="";
	String TIME="";
	SharedPreferences sharedPreferences;
	ListView listView;
	TextView textView;
	ArrayList<String> valueStrings;
	ArrayAdapter<String> adapter;
	int page=0;
	private LinearLayout RegisterView;
	private RelativeLayout loadViewLayout;
	private RelativeLayout errorLayout;
	private boolean STATE=false;
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
	super.onCreate(savedInstanceState);
	setContentView(R.layout.qiandao);
	context=this;
	button=(Button)findViewById(R.id.qiandao);
	RegisterView=(LinearLayout)findViewById(R.id.registerView);
	loadViewLayout=(RelativeLayout)findViewById(R.id.loadlayout);
	errorLayout=(RelativeLayout)findViewById(R.id.errorlayout);
	handler=new QHandler();
	 sharedPreferences=context.getSharedPreferences("XSM", 0);
	user=sharedPreferences.getString("User","");
	listView=(ListView)findViewById(R.id.rp);
	textView=(TextView)findViewById(R.id.nop);
	valueStrings=new ArrayList<String>();
	adapter=new ArrayAdapter<String>(context, R.layout.qiantext,valueStrings);
	listView.setAdapter(adapter);
	initView();
}

protected void onDestroy() {
    STATE=false;
	super.onDestroy();
}
private void  initView(){
	STATE=true;
	loadViewLayout.setVisibility(View.VISIBLE);
	errorLayout.setVisibility(View.GONE);
	new Thread(new GetMessage()).start();
}

public void Back(View view){
	finish();
	STATE=false;
}
public void QianDao(View view){
	button.setEnabled(false);
	new Qian().execute();
}



class  Qian extends AsyncTask<Void, Void, String>{
	protected String doInBackground(Void... params) {
		List param=new ArrayList();
		param.add(new BasicNameValuePair("ASK","RegisterForJindou"));
		param.add(new BasicNameValuePair("userid",user));
		PostMessage postMessage=new PostMessage(param);
		String result=postMessage.getResult();
	    return result;
	}
	
	protected void onPostExecute(String result) {
         if(result.equals("-1")){
		 Toast.makeText(context,R.string.neterror, Toast.LENGTH_LONG).show();
		 button.setEnabled(true);	
		}
		else if(result.equals("-2")){
			Toast.makeText(context, "还没到签到时间", Toast.LENGTH_LONG).show();
			button.setEnabled(true);	
		}else{		
			Toast.makeText(context, "签到成功!金豆+"+result, Toast.LENGTH_LONG).show();
		 }

		super.onPostExecute(result);
	}
}


    class GetMessage implements Runnable{
		public void run() {
		while(STATE==true){
			page=valueStrings.size();
			List param=new ArrayList();
			param.add(new BasicNameValuePair("ASK","GetRegisterPersion"));
			PostMessage postMessage=new PostMessage(param);
			String result=postMessage.getResult();
			try{
				Message msg=handler.obtainMessage();
				if(!result.equals("-1")){
				JSONArray array=new JSONArray(result);
				if(array.length()>0)
				{		
		for(int i=0;i<array.length();i++){
			JSONObject object=array.optJSONObject(i);
			SimpleDateFormat dateFormat=new SimpleDateFormat("HH:mm:ss");
			String timeString=dateFormat.format(Long.parseLong(object.get("time")+""));
			String vString=object.getString("name")+"在"+timeString+"签到成功!";	
			valueStrings.add(0,vString);
		}
		 msg.what=2;
		}else {
		 msg.what=1;
		}	
		 }else {
		 msg.what=-1;
		 }
		handler.sendMessage(msg);
		}catch(Exception e){
		Log.d("my", e+"");
		}	
			STATE=false;
		}
		}
    }
    
public void Update(View view){
	initView();
}
    class QHandler extends Handler{
    	public void handleMessage(Message msg) {
    		Log.d("my",msg.what+"");
    		loadViewLayout.setVisibility(View.GONE);
          if(msg.what==2){
        	  RegisterView.setVisibility(View.VISIBLE);
        	  textView.setVisibility(View.GONE);
        	  listView.setVisibility(View.VISIBLE);
        	  adapter.notifyDataSetChanged();  
          }else if(msg.what==1){
        	RegisterView.setVisibility(View.VISIBLE);
        	listView.setVisibility(View.GONE);
        	textView.setVisibility(View.VISIBLE);
            textView.setText("还没有人签到");
          }else if(msg.what==-1){
        	  errorLayout.setVisibility(View.VISIBLE);
        	  loadViewLayout.setVisibility(View.GONE);
          }
    		super.handleMessage(msg);
    	}
    }
}
